home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / JAVA Utilities / JProxy 1.1.0 / SAMPLES.JAR / com / jproxy / samples / ejb / test / PerformanceClient.java < prev    next >
Encoding:
Java Source  |  2003-04-29  |  7.0 KB  |  262 lines

  1. package com.jproxy.samples.ejb.test;
  2. /*
  3. usage:
  4. java -cp samples.jar;proxyclient.jar:j2api.jar [-Djava.properties]
  5.  com.jproxy.samples.ejb.test.PerformanceClient
  6.  [ [http://|https://]YourServerName:[port] [#_of_loops] [#_of_bytes] [#_of_threads] ]
  7.  
  8. where:
  9.  #_of_loops - number of loops. Default 1"+
  10.  #_of_bytes - number of bytes send(received) to host in request. Default 0"+
  11.  #_of_threads - number of threads. Default 1"+
  12.  
  13. usage example: Performance test WITH JProxy
  14. 10 loops, 10000 bytes, 100 threads (10*100 calls, 10*100*10000 sent bytes, 10*100*10000 received)
  15. %JAVA_HOME%\bin\java -cp samples.jar;proxyclient.jar;j2api.jar \
  16.  -Djava.naming.factory.initial=com.jproxy.proxy.NamingContextFactory \
  17.  -Djava.naming.provider.url=localhost:8080 \
  18.  com.jproxy.samples.ejb.test.PerformanceClient \
  19.  10 10000 100
  20.  
  21. usage example: Performance test WITHOUT JProxy (JBoss)
  22. 10 loops, 10000 bytes, 100 threads (10*100 calls, 10*100*10000 sent bytes, 10*100*10000 received)
  23. %JAVA_HOME%\bin\java -cp samples.jar; \
  24.  %LIB_PATH%/jboss-j2ee.jar;%LIB_PATH%/jboss-client.jar;\
  25.  %LIB_PATH%/jnp-client.jar;%LIB_PATH%/jbosssx-client.jar;\
  26.  %LIB_PATH%/jboss-common-client.jar;%LIB_PATH%/log4j.jar \
  27.  -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory \
  28.  -Djava.naming.provider.url=localhost \
  29.  com.jproxy.samples.ejb.test.PerformanceClient \
  30.  10 10000 100
  31. */
  32. import javax.naming.*;
  33. import java.applet.*;
  34. import java.util.*;
  35. import java.io.*;
  36.  
  37. import com.jproxy.proxy.*;
  38. import com.jproxy.samples.interfaces.ITest;
  39.  
  40. public class PerformanceClient
  41.     extends SessionClient
  42.     implements Runnable
  43. {
  44.     static String sync = "";
  45.  
  46.     int bytes = 0;
  47.     int loops = 1;
  48.     int loop = 0;
  49.     int index = 0;
  50.     float delay = 0;
  51.     float minDelay = 0;
  52.     float maxDelay = 0;
  53.     byte[] buf = null;
  54.     String log = "";
  55.     Thread thread = new Thread(this);
  56.     String error = null;
  57.  
  58.     public PerformanceClient()
  59.     {
  60.     }
  61.  
  62.  
  63.     void fillBuffer()
  64.     {
  65.         /* to test remote exceptions thrown by EJB */
  66.         if(bytes<0)
  67.         {
  68.             buf = null;
  69.             return;
  70.         }
  71.         buf = new byte[bytes];
  72.         for(int i=0; i<bytes; i++)
  73.             buf[i] = (byte)(i & 0xFF);
  74.     }
  75.  
  76.     public void test()
  77.         throws Exception
  78.     {
  79.         fillBuffer();
  80.  
  81.         long t = System.currentTimeMillis();
  82.  
  83.         for(int i=0; i<loops; i++)
  84.         {
  85.             long tt = System.currentTimeMillis();
  86.             session.echoBytes(buf);
  87.             float d = (float)((System.currentTimeMillis()-tt)/1000.0);
  88.             if(d<minDelay)
  89.                 minDelay = d;
  90.             else if(d>maxDelay)
  91.                 maxDelay = d;
  92.             loop++;
  93.         }
  94.         delay = (float)((System.currentTimeMillis()-t)/1000.0);
  95.         log +=
  96.             "\r\n*****************************************************"+
  97.             "\r\nURL:                          "+env.getProperty(Context.PROVIDER_URL)+
  98.             "\r\nNumber of loops:              "+loops+
  99.             "\r\nNumber of sent and received bytes per loop: "+bytes+
  100.             "\r\nThread number:                "+index+
  101.             "\r\nMin time for loop: "+minDelay+" seconds"+
  102.             "\r\nMax time for loop: "+maxDelay+" seconds"+
  103.             "\r\nAverage time for loop: "+delay/loops+" seconds"+
  104.             "\r\nTotal time for All loops: "+delay+" seconds"+
  105.             "\r\n*****************************************************";
  106.     }
  107.  
  108.     static void println(String msg)
  109.     {
  110.         synchronized(sync)
  111.         {
  112.             System.out.println(msg);
  113.         }
  114.     }
  115.  
  116.     public void run()
  117.     {
  118.         try{
  119.             init();
  120.             test();
  121.             destroy();
  122.             println(log);
  123.         }
  124.         catch(Exception e){
  125.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  126.             PrintWriter writer = new PrintWriter(baos);
  127.             e.printStackTrace(writer);
  128.             writer.flush();
  129.             error = new String(baos.toByteArray());
  130.             println("\r\n########### ERROR in thread "+index+"###########\r\n"+error);
  131.             System.exit(1);
  132.         }
  133.     }
  134.  
  135.  
  136.     public static void main(String[] arguments)
  137.     {
  138.         int threads = 1;
  139.         String hostUrl = null;
  140.         int loops = 1;
  141.         int bytes = 0;
  142.  
  143.         Vector vargs = new Vector();
  144.         for(int i=0; i<arguments.length; i++)
  145.             if(arguments[i].startsWith("-D"))
  146.             {
  147.                 int p = arguments[i].indexOf("=");
  148.                 System.setProperty(arguments[i].substring(2, p), arguments[i].substring(p+1));
  149.                 System.out.println("key = "+arguments[i].substring(2, p)+" val = "+arguments[i].substring(p+1));
  150.             }
  151.             else
  152.                 vargs.add(arguments[i]);
  153.         String args[] = (String[])vargs.toArray(new String[]{});
  154.  
  155.         if(args.length==0)
  156.         {
  157.             System.out.println("usage: \r\njava -cp samples.jar;proxyclient.jar [-Djava.properties] "+
  158.                 "com.jproxy.samples.ejb.test.PerformanceClient "+
  159.                 "[#_of_loops] [#_of_bytes] [#_of_threads]"+
  160.                 "\r\n#_of_loops - number of loops. Default 1"+
  161.                 "\r\n#_of_bytes - number of bytes send(received) to host in request. Default 0"+
  162.                 "\r\n#_of_threads - number of threads. Default 1"+
  163.                 "\r\nWill try \"localhost\" ...");
  164.         }
  165.         if(args.length>0)
  166.         {
  167.             try{
  168.                 loops = Integer.parseInt(args[0]);
  169.                 if(loops<=0)
  170.                     throw new Exception();
  171.             }catch(Exception e){
  172.                 System.out.println("Wrong #_of_loops parameter!");
  173.                 System.exit(1);
  174.             }
  175.         }
  176.         if(args.length>1)
  177.         {
  178.             try{
  179.                 bytes = Integer.parseInt(args[1]);
  180.                 if(bytes<0)
  181.                     throw new Exception();
  182.             }catch(Exception e){
  183.                 System.out.println("Wrong #_of_bytes parameter!");
  184.                 System.exit(1);
  185.             }
  186.         }
  187.         if(args.length>2)
  188.         {
  189.             try{
  190.                 threads = Integer.parseInt(args[2]);
  191.                 if(threads<=0 | threads>1024)
  192.                     throw new Exception();
  193.             }catch(Exception e){
  194.                 System.out.println("Wrong #_of_threads!");
  195.                 System.exit(1);
  196.             }
  197.         }
  198.  
  199.  
  200.         long time = System.currentTimeMillis();
  201.         PerformanceClient[] clients = new PerformanceClient[threads];
  202.         for(int t=0; t<threads; t++)
  203.         {
  204.             clients[t] = new PerformanceClient();
  205.  
  206.             clients[t].env = System.getProperties();
  207.             clients[t].loops = loops;
  208.             clients[t].bytes = bytes;
  209.             clients[t].index = t;
  210.  
  211.             clients[t].thread.start();
  212.         }
  213.         try{
  214.             for(int t=0; t<threads; t++)
  215.                 if(clients[t].thread.isAlive())
  216.                     clients[t].thread.join();
  217.         }
  218.         catch(InterruptedException e){
  219.             e.printStackTrace();
  220.         }
  221.         float totalTime = (float)((System.currentTimeMillis()-time)/1000.0);
  222.  
  223.         int errorIndex = -1;
  224.         for(int t=0; t<threads; t++)
  225.             if(clients[t]!=null && clients[t].error!=null)
  226.             {
  227.                 errorIndex = t;
  228.                 break;
  229.             }
  230.         if(errorIndex==-1)
  231.         {
  232.             float minDelay = 0;
  233.             float maxDelay = 0;
  234.             float averageDelay = 0;
  235.  
  236.             for(int t=0; t<threads; t++)
  237.             {
  238.                 if(minDelay>clients[t].minDelay)
  239.                     minDelay = clients[t].minDelay;
  240.                 else if(maxDelay<clients[t].maxDelay)
  241.                     maxDelay = clients[t].maxDelay;
  242.                 averageDelay += clients[t].delay/loops;
  243.             }
  244.             averageDelay /= threads;
  245.             String msg = "\r\nTest executed seccessfully. No errors."+
  246.             "\r\n*****************************************************"+
  247.             "\r\nURL:                      "+clients[0].hostUrl+
  248.             "\r\nNumber of loops:          "+loops+
  249.             "\r\nNumber of sent and received bytes per loop: "+bytes+
  250.             "\r\nTotal number of Threads:  "+threads+
  251.             "\r\nTotal Number of sent and received bytes: "+bytes*threads*loops+
  252.             "\r\nMin request time:         "+minDelay+" seconds"+
  253.             "\r\nMax request time:         "+maxDelay+" seconds"+
  254.             "\r\nAverage request time:     "+averageDelay+" seconds"+
  255.             "\r\nTotal time:               "+totalTime+" seconds"+
  256.             "\r\n*****************************************************";
  257.             println(msg);
  258.         }
  259.         else
  260.             println("Error during test execution!\r\n"+clients[errorIndex].error);
  261.     }
  262. }